/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "Vector4.h"
#include "MathConfig.h"

namespace Hogshead
{
	namespace Common
	{
		class Quaternion;

		/**
		* This class represents a 4x4 affine transformation matrix. The internal data is stored row continguous while 
		* interfacing with this class is done based on column major rules. There are many operations implemented for
		* convenience.
		*
		* http://cache-www.intel.com/cd/00/00/29/37/293748_293748.pdf
		*
		* @see Vector3
		* @see Quaternion
		*/
		class Matrix4
		{
		public:
			Matrix4();
			Matrix4(const Matrix4& other);
			Matrix4(const Quaternion& in_orientation);
			Matrix4(float,float,float,float,
					float,float,float,float,
					float,float,float,float,
					float,float,float,float);
			Matrix4(float* in_values);
			~Matrix4();
			__forceinline float operator()(int i, int j) const
			{
				return _data[4 * i + j];
			}
			__forceinline float& operator()(int i, int j)
			{
				return _data[4 * i + j];
			}
			operator const float*() const;
			Matrix4& operator=(const Matrix4 m);
			Matrix4 operator*(const Matrix4& right) const;
			Vector4 friend operator*(const Matrix4& left, const Vector4& right);
			
			bool setRow(int i, float c0, float c1, float c2, float c3);
			bool setColumn(int j, float r0, float r1, float r2, float r3);
			bool setColumn(int j, const Vector3& col, float r3);
			Vector4 getColumn(int j) const;
			Vector4 getRow(int i) const;

			//scale

			//extract rotation
			enum ROTATION_OPTION{RO_1, RO_2};
			Vector3 getRotation(float in_prefered_z = 0.0f, ROTATION_OPTION in_option = RO_1) const;

			Quaternion getRotationQuaternion() const;

			//three basis right-handed
			Vector3 getForward() const;//z
			Vector3 getUp() const;//y
			Vector3 getRight() const;//x
			Vector3 getScale() const;
			void setForward(const Vector3& in_forward );
			void setUp(const Vector3& in_up);
			void setRight(const Vector3& in_right);

			//and translation
			Vector3 getTranslation() const;
			void setTranslation(const Vector3& in_translation);

			//inverse
			/**
			Warning: This assumes that the Matrix is an Affine transform (ie, scale, orientation, translation only)
			**/
			Matrix4 getInverse() const;
			// slow inverse
			/**
			Calculates the inverse of the matrix.  This will work for any non-singular matrix.  If this matrix is
			singular, the method returns false
			@param out_matrix The place to store this matrix's inverse
			@return true if successfully inverted, false otherwise
			**/
			bool getSlowInverse(Matrix4& out_matrix) const;
			
			
			Matrix4& scale(float x, float y, float z);
			Matrix4& scale(const Vector3& in_scales);
			Matrix4& scale(float xyz);

			

			//types of matrix creation
			
			static Matrix4 identity();
			static Matrix4 createPerspectiveProjection(const float& in_horizontal_fov,
				const float& in_aspect_ratio,
				const float& in_near_clip,
				const float& in_far_clip);

			static Matrix4 createOrthographicProjection(const float& width,
				const float& height,
				const float& far_clip,
				const float& near_clip);

			static Matrix4 createSRT(const float& scale_x, const float& scale_y, const float& scale_z,
				const float& angle_about_x, const float& angle_about_y, const float& angle_about_z,
				const float& translation_x, const float& translation_y, const float& translation_z);

			static Matrix4 createSRT(float scale_x, float scale_y, float scale_z, const Vector3& orientation, const Vector3& translation);
			

			Matrix4& toIdentity();
			Matrix4& tozero();

			friend PrintStream& operator<<(PrintStream& in_stream, const Matrix4& mat4);
		private:
			static Matrix4 sse41_multiply(const Matrix4& left, const Matrix4& right);
			static Matrix4 sse3_multiply(const Matrix4& left, const Matrix4& right);
			static Matrix4 slow_multiply(const Matrix4& left,const Matrix4& right);

			static Matrix4 (*matrix_multiply)(const Matrix4&, const Matrix4&);

			friend class Math;
			float _data[16];

			
		};
	}
}